﻿/*
 * FeatureSpatialFilterSet
 * 
 * initial implementation : 5.12.2008
 *
 * Copyright (c) Adam Sida, asida@centrum.cz.
 * All rights reserved.
 * 
 * for license see Petra.Gis.License.txt
 */
using System;
using System.Collections.Generic;
using Petra.Gis.Geometry.Support;

namespace Petra.Gis.DataServices
{

  /// <summary>
  /// Stores Feature set spatial filter. Single Feature can contain multiple geometries
  /// but one spatial filter for one of spatial field is sufficent to limit amount of data at output.
  /// Spatial filter must be specified in spatial field coordinate system.
  /// </summary>
  public struct FeatureSpatialFilter // : Dictionary<string, BoundingBox>
  {

    /// <summary>
    /// Rectangular area in feature, data source coordinate system units
    /// for vector base spatial data used to limit amount of output data
    /// for analysis or display in map. It is recomended to use this Bounds
    /// as coarse MBR overlap spatial filter inside data source as this is usualy fastest.
    /// </summary>
    public BoundingBox Bounds { get; set; }




    /// <summary>
    /// name of spatial field that is this filter related to.
    /// If feature set contains multiple spatial fields,
    /// value must be set to valid spatial field name.
    /// </summary>
    public string SpatialFieldName { get; set; }




    /// <summary>
    /// Number of Grid cells/pixels per feature coordinate system storage unit.
    /// <para>
    /// For grid base spatial data, is this value aditional information to Bounds value of spatial filter.
    /// Many Grid data feature sets are organized into levels, where each level is prepared
    /// for specific map resolution or grid analysis precision. Whenever Grid data are selected,
    /// this value needs to be specified. If this value is not set, some data sources or feature sets
    /// might throw error as they get unable to pick appropriate data. Some of them offers instead data
    /// with lowest possible resolution.
    /// </para>
    /// </summary>
    public double GridResolution { get; set; }



    public override string ToString()
    {

      string tmp = "Bounds:" + Bounds.ToString()
        + ";SpatialFieldName:" + (SpatialFieldName == null ? "" : SpatialFieldName)
        + ";GridResolution" + GridResolution.ToString();

      return tmp;
    }



    public static FeatureSpatialFilter Parse(string value)
    {
      string[] tmp = value.Split(';', ':');
      FeatureSpatialFilter f;

      if (tmp.Length != 6)
        throw new ArgumentException("value " + value + " cant convert to FeatureSpatialFilter");

      else
      {
        f = new FeatureSpatialFilter()
        {
          Bounds = BoundingBox.Parse(tmp[1]),
          SpatialFieldName = tmp[3],
          GridResolution = Double.Parse(tmp[5])
        };
      }
      return f;
    }


  }

}
